const app = getApp();
let that;
Page({
  data: {
    expreIndex: '',
    entering: '智能录入',
    enteringFalg: true,
    capacity: true,
    tradition: false,
    guanbi: true,
    recipientsName: '',
    recipientsPhone: '',
    receiverRegion: ['省份..', '城市..', '区县..'],
    recipientsSite: '',
    infomation: '',
    senderNmae: '',
    senderPhone: '',
    senderSite: '',
    goodsIndex: '',
    remak: ''
  },
  onLoad: function() {
    that = this;
    let UserID = "201701031229AA";
    let ShopID = "HBWH0002";
    //获取本店铺支持电子面单的快递公司列表
    wx.request({
      url: 'https://m.ehomebar.com/baas/dianjia/expressenhance/getEorderExpListInfo',
      data: {
        ShopID: ShopID
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        let result = res.data.result;
        if (result === 1) {
          let expressCompany = res.data.ExpressCorpName;
          let expreArray = ['中通快递', '圆通快递', '韵达快递', '申通快递', '百世汇通'];
          let expreIndex = 0;
          for (let i = 0; i < expreArray.length; i++) {
            if (expreArray[i] == expressCompany) {
              expreIndex = i;
            }
          }
          that.setData({
            expreIndex: expreIndex
          })
        }
      },
      fail: () => {
        console.log("调用失败");
      },
      complete: () => {
        // complete
      }
    })
    //获取寄件人信息
    wx.request({
      url: 'https://m.ehomebar.com/baas/dianjia/expressenhance/getUserDefAddr',
      data: {
        UserID: UserID
      },
      method: 'POST',
      header: {
        'content-tyep': 'applacation/json'
      },
      success: res => {
        console.log(res)
        let senderNmae = res.data.AddressName;
        let senderPhone = res.data.AddressMobile;
        let senderSite = res.data.AddressDescription;
        that.setData({
          senderNmae: senderNmae,
          senderPhone: senderPhone,
          senderSite: senderSite,
        })
      },
      fail: () => {
        console.log("调用失败");
      },
      complete: () => {
        //调用完后出发
      }
    })
  },
  Back: () => {
    wx.navigateBack({
      delta: 1
    })
  },
  history: () => {
    wx.navigateTo({
      url: 'history'
    })
  },
  onCompany: function(e) {
    this.setData({
      expreIndex: e.detail.expreIndex
    })
  },
  // voice: () => {

  // },
  camera: options => {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        let base64 = wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], 'base64')
        console.log(base64)
      },
      fail: res => {
        console.log(res.errMsg)
      },
      complete: () => {

      }
    })
  },
  Entering: function() {
    if (this.data.enteringFalg) {
      this.setData({
        entering: '传统录入',
        enteringFalg: false,
        capacity: false,
        tradition: true
      })
    } else {
      this.setData({
        entering: '智能录入',
        enteringFalg: true,
        capacity: true,
        tradition: false
      })
    }
  },
  discern: function() {
    let info = this.data.infomation
    wx.request({
      url: 'https://www.ehomebar.com/baas/dianjia/expressenhance/textAddrPhase',
      data: {
        textPhaser: info
      },
      method: 'post',
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        console.log(res)
        let Province = res.data.Province;
        let CityName = res.data.CityName;
        let AreaName = res.data.AreaName;
        let newAddr = [Province, CityName, AreaName];
        let Receiver_Name = res.data.RealName;
        let Receiver_Mobile = res.data.Mobile;
        let Receiver_Address = res.data.AddressDetail;
        this.setData({
          recipientsName: Receiver_Name,
          recipientsPhone: Receiver_Mobile,
          recipientsSite: Receiver_Address,
          receiverRegion: newAddr
        })
        this.Entering()
      },
      fail: err => {
        console.log(err)
        console.log("调用失败");
      },
      complete: function() {
        // complete
        wx.showModal({
          title: '提醒',
          content: '请仔细核对识别的信息，如果误差请手动修改',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },
  relation: () => {
    wx.navigateTo({
      url: 'relation'
    })
  },
  getRecipientsName: function(e) {
    this.setData({
      recipientsName: e.detail.value
    })
  },
  getRecipientsPhone: function(e) {
    this.setData({
      recipientsPhone: e.detail.value
    })
  },
  getRecipientsSite: function(e) {
    this.setData({
      recipientsSite: e.detail.value
    })
  },
  getInfomation: function(e) {
    this.setData({
      infomation: e.detail.value
    })
  },
  getSenderName: function(e) {
    this.setData({
      senderNmae: e.detail.value
    })
  },
  getSenderPhone: function(e) {
    this.setData({
      senderPhone: e.detail.value
    })
  },
  getSenderSite: function(e) {
    this.setData({
      senderSite: e.detail.value
    })
  },
  onSite: function(e) {
    this.setData({
      receiverRegion: e.detail.receiverRegion,
    })
  },
  onDescribe: function(e) {
    this.setData({
      goodsIndex: e.detail.goodsIndex
    })
  },
  getRemak: function(e) {
    this.setData({
      remak: e.detail.value
    })
  },
  submit: function() {
    let ShopID = "HBWH0002";
    let UserID = "201701031229AA";
    let appCustomerID = "dianjiaC001";
    let expressCompany = this.data.expreIndex;
    let recipientsName = this.data.recipientsName;
    let recipientsPhone = this.data.recipientsPhone;
    let recipientsSite = this.data.recipientsSite;
    let receiverProvinceName = this.data.receiverRegion[0];
    let receiverCityName = this.data.receiverRegion[1];
    let receiverExpAreaName = this.data.receiverRegion[2];
    let senderNmae = this.data.senderNmae;
    let senderPhone = this.data.senderPhone;
    let senderSite = this.data.senderSite;
    let goodsName = this.data.goodsIndex;
    let remak = this.data.remak;
    //判断填写信息
    if (!expressCompany || expressCompany == '请选择') {
      wx.showToast({
        title: '请选择快递公司！',
        icon: 'none',
        duration: 2000
      });
    } else
    if (!recipientsName || !recipientsPhone || !recipientsSite || !receiverProvinceName || receiverProvinceName == '省份..' || !receiverCityName || receiverCityName == '城市..' || !receiverExpAreaName || receiverExpAreaName == "区县..") {
      wx.showToast({
        title: '请正确填写收件人信息！',
        icon: 'none',
        duration: 2000
      });
    } else
    if (!senderNmae || !senderPhone || !senderSite) {
      wx.showToast({
        title: '请正确填写寄件人信息！',
        icon: 'none',
        duration: 2000
      });
    } else
    if (!goodsName || !remak || goodsName == '请选择..') {
      wx.showToast({
        title: '请正确填写物品信息！',
        icon: 'none',
        duration: 2000
      });
    } else {
      //提交请求 
      wx.request({
        url: 'https://m.ehomebar.com/baas/dianjia/expressenhance/newExpOrderbyExpName',
        data: {
          ShopID: ShopID,
          UserID: UserID,
          StaffID: '',
          ExpressCorpName: expressCompany,
          Receiver_Name: recipientsName,
          Receiver_Mobile: recipientsPhone,
          Receiver_ProvinceName: receiverProvinceName,
          Receiver_CityName: receiverCityName,
          Receiver_ExpAreaName: receiverExpAreaName,
          Receiver_Address: recipientsSite,
          Sender_Name: senderNmae,
          Sender_Mobile: senderPhone,
          Sender_Address: senderSite,
          GoodsName: goodsName,
          GoodsDesc: remak,
          appCustomerID: appCustomerID
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res => {
          var KdOrderID = res.data.KdOrderID;
          wx.showToast({
            title: '快递下单成功！',
            icon: 'success',
            duration: 2000
          });
          wx.showActionSheet({
            itemList: ['自己送驿站', '上门收快递'],
            success: res => {
              let expFlag;
              res.tapIndex === 0 ? expFlag = 1 : expFlag = 2;
              wx.request({
                url: 'https://www.ehomebar.com/baas/dianjia/expressenhance/setSendExpModel',
                data: {
                  KdOrderID: KdOrderID,
                  PickupModel: expFlag
                },
                mehtod: 'POST',
                herder: {
                  'content- type': 'application/json'
                },
                success: res => {
                  wx.redirectTo({
                    url: 'finishnewexp?expFlag=' + expFlag
                  });
                },
                fail: () => {
                  console.log("调用失败");
                },
                complete: () => {
                  // complete
                }
              })
            },
            fail: err => {
              console.log(err.errMsg)
            }
          })
        },
        fail: () => {
          console.log("调用失败");
        },
        complete: () => {
          // complete
        }
      })
    }
  },
})